#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
#include "InetAddr.hpp"
const std::string sep = ": ";
using namespace LogMoudle;
class gop
{
public:
    gop(std::string name = "./dictionary.txt")
        : _path_name(name)
    {
    }
    bool filemassage()
    {
        std::ifstream in(_path_name);
        if (!in.is_open())
        {
            LOG(loglevel::DEBUG) << "打开字典: " << _path_name << " 错误";
            return false;
        }
        std::string line;
        while (getline(in, line))
        {
            auto e = line.find(':');
            if (e == std::string::npos)
            {
                continue;
            }
            std::string english = line.substr(0, e);
            std::string hanyu = line.substr(e + 1);
            if (english.empty() || hanyu.empty())
            {
                LOG(loglevel::WARNING) << "没有有效内容: " << line;
                continue;
            }
            as.insert(std::make_pair(english, hanyu));
            LOG(loglevel::DEBUG) << "加载: " << line;
        }
        in.close();
        return true;
    }
    std::string translate(const std::string &q, InetAddr &client)
    {
        auto e = as.find(q);
        if (e != as.end())
        {
             LOG(loglevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << q<< "->" << e->second;
            return as[q];
        }
         LOG(loglevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << q << "->None";
            return "None";
    }
    ~gop()
    {
    }

private:
    std::string _path_name;// 路径+文件名
    std::unordered_map<std::string, std::string> as;
};